import com.alibaba.fastjson.JSON;
import com.google.common.base.Converter;
import com.tc.converter.GuavaConverterHolder;
import com.tc.entity.TestConverterBean1;
import com.tc.entity.TestConverterBean2;
import com.tc.entity.TestMapperBean1;
import com.tc.entity.TestMapperBean2;
import com.tc.support.BeanMapper;
import com.tc.support.BeanMapperHelper;
import lombok.extern.java.Log;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/mvc-servlet.xml" }, inheritLocations = true)
@ActiveProfiles(value = "test")
@Log
public class testMapperAndConverter {

    @Autowired
    private BeanMapper beanMapper;
    /**
     * 测试bean的转换
     */
    @Test
    public void demo4(){ //Mapper 例子
        TestMapperBean1 testMapperBean1 =new TestMapperBean1();
        testMapperBean1.setAmount(100l);
        testMapperBean1.setAmount2(200l);
        TestMapperBean2 testMapperBean2 = BeanMapperHelper.getInstance().map(testMapperBean1, TestMapperBean2.class);
        log.info( ""+testMapperBean2.getAmount());
        log.info( ""+testMapperBean2.getAmount2());
        //System.out.println(s.getName());
        //s.getName();
    }

    @Test
    public void demo(){ //Mapper bean转换---通过自定义Mapper'OrikaCustomMapper'
        TestConverterBean1 tb=new TestConverterBean1();
        tb.setAge("18");
        tb.setName("田才");
        TestMapperBean1 testMapperBean1 =new TestMapperBean1();
        testMapperBean1.setAmount(100l);
        testMapperBean1.setAmount2(200l);
        List<TestMapperBean1> tmbs=new ArrayList<>();
        tmbs.add(testMapperBean1);
        tb.setAmount1(tmbs);
        TestConverterBean2 map = beanMapper.map(tb, TestConverterBean2.class);
        log.info(JSON.toJSONString(map));
        TestConverterBean1 map1 = beanMapper.map(map, TestConverterBean1.class);
        log.info(JSON.toJSONString(map1));
    }

    @Test
    public void demo1(){ //guava bean转换。
        TestConverterBean1 tb=new TestConverterBean1();
        tb.setAge("18");
        tb.setName("田才");
        TestMapperBean1 testMapperBean1 =new TestMapperBean1();
        testMapperBean1.setAmount(100l);
        testMapperBean1.setAmount2(200l);
        List<TestMapperBean1> tmbs=new ArrayList<>();
        tmbs.add(testMapperBean1);
        tb.setAmount1(tmbs);
        Converter<TestConverterBean1, TestConverterBean2> myType = GuavaConverterHolder.getConverter("myType");
        TestConverterBean2 convert2 = myType.convert(tb);
        log.info(JSON.toJSONString(convert2));
        TestConverterBean1 convert1 = myType.reverse().convert(convert2);
        log.info(JSON.toJSONString(convert1));
    }
    //总结guava bean转换 优点比较灵活缺点需要写许多代码
    //Mapper bean转换,节省代码,方便，但是灵活有于。
}
